<script setup>

//定义的各种数据
import {reactive, ref} from "vue";
import xx from "@/axios/xx";
import axios from "axios";
import {ElMessage, genFileId} from "element-plus";

const info = reactive({
  list:[],//列表数据
  form:{},//表单数据
  dialogVisible:false,//对话框数据
  pageNum:1,//当前页
  pageSize:5, //每页大小
  total:0,//总条数
  isAdd:true,//增加和修改标识
  loanProductTypeList:[],//分类信息所有数据
  bankInfoList:[],//银行信息数据
  ImportDialogVisible:false,//导入弹窗
  ImportUrl:''//导入的地址
})
info.ImportUrl = xx.defaults.baseURL + 'loanProduct/importExcel'
//查询列表信息
const load = () => {
  xx.get('/loanProduct',{
    params:{
      pageNum:info.pageNum,
      pageSize:info.pageSize,
      productName:info.form.productName,
      interestRate:info.form.interestRate
    }
  }).then(res => {
    console.log(res)
    info.list = res.data.list
    //当前页
    info.pageNum=res.data.pageNum
    //每页大小
    info.pageSize=res.data.pageSize
    //总条数
    info.total=res.data.total
  })
}
load()

//分页，当前页改变
const handleCurrentChange = (pageNum) => {
  info.pageNum=pageNum
  load()
}
//每页大小
const handleSizeChange = (pageSize) => {
  info.pageSize=pageSize
  load()
}

//增加按钮点击方法
const add = () => {
  //查询分类信息
  xx.get('/loanProductType/queryAll').then(res => {
    info.loanProductTypeList = res.data
  })
  //fixme 替换地址
  xx.get('/bankInfo/queryAll').then(res => {
    info.bankInfoList = res.data
  })
  info.dialogVisible = true
  info.isAdd = true
}

//持久化数据
const persistence = () => {
  xx({
    url:'/loanProduct',
    method:info.isAdd?'post':'put',
    data:info.form
  }).then(res => {
    if(res.code === 200){
      ElMessage({
        message:res.message,
        type:'success',
      })
    }else {
      ElMessage.error(res.message)
    }
    info.dialogVisible = false
    info.form = {}//清空数据
    load()
  })
}

//关闭对话框，清除数据
const handleClose = () => {
  info.form = {}
  info.dialogVisible = false
}

//点击修改按钮-根据ID查询对应数据
const handleEdit = (productId) => {
  info.isAdd = false
  xx.get('loanProduct/'+productId).then(res => {
    info.form = res.data
    info.dialogVisible = true
  })
  //查询分类信息
  xx.get('/loanProductType/queryAll').then(res => {
    info.loanProductTypeList = res.data
  })
  //fixme 替换地址
  xx.get('/bankInfo/queryAll').then(res => {
    info.bankInfoList = res.data
  })
}

//删除
const handleDelete = (productId) => {
  xx.delete('/loanProduct/'+productId).then(res => {
    if(res.code === 200){
      ElMessage({
        message:res.message,
        type:'success',
      })
    }else {
      ElMessage.error(res.message)
    }
    load()
  })
}
//导出数据
const exportExcel = () => {
  const load = () => {
    xx({
      method: "get",
      url: "/loanProduct/exportExcel",
      params:{
        pageNum:info.pageNum,
        pageSize:info.pageSize,
        productName:info.form.productName,
        interestRate:info.form.interestRate
      },
      responseType: "blob", //定义为blob
    }).then(res => {
      let blob = new Blob([res])
      let downloadElement = document.createElement('a')
      let href = window.URL.createObjectURL(blob) // 创建下载的链接
      downloadElement.href = href
      downloadElement.download = '贷款产品信息.xlsx' // 下载后文件名
      document.body.appendChild(downloadElement)
      downloadElement.click() // 点击下载
      document.body.removeChild(downloadElement) // 下载完成移除元素
      window.URL.revokeObjectURL(href) // 释放掉blob对象
    })
  }
  load()
}
//导入数据
const importExcel = () => {
  info.ImportDialogVisible = true
}
const upload = ref()
//导入数据处理文件覆盖
const handleExceed = (files) => {
  upload.value.clearFiles()
  const file = files[0]
  file.uid = genFileId()
  upload.value.handleStart(file)
}
//确认上传
const submitUpload = () => {
  upload.value.submit()
}
//上传之前控制
const beforeUpload = (rawFile) => {
  if (rawFile.type !== 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
    ElMessage.error('只能上传xlsx类型!')
    return false
  }
  return true
}
//上传成功之后控制
const handleSuccess = (response,uploadFile) => {
  if (response.code === 200){
    ElMessage({
      message:response.message,
      type:'success',
    })
  }else {
    ElMessage.error(response.message)
  }
  info.ImportDialogVisible = false
  load()
  upload.value.clearFiles()
}
</script>

<template>

  <!--增加按钮-->
  <el-button color="#68606d" :dark="isDark" @click="add">增加产品</el-button>
  <el-button color="#68606d" :dark="isDark" @click="exportExcel">导出数据</el-button>
  <el-button color="#68606d" :dark="isDark" @click="importExcel">导入数据</el-button>
  <br><br>
  <!--快捷查询-->
  <el-form :inline="true" :model="info.form" class="demo-form-inline">
    <el-form-item label="产品名称">
      <el-input v-model="info.form.productName" placeholder="模糊查询" clearable />
    </el-form-item>
    <el-form-item label="产品利率">
      <el-input v-model="info.form.interestRate" placeholder="精准查询" clearable />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="load">快捷查询</el-button>
    </el-form-item>
  </el-form>
  <!--表格信息-->

  <el-table :data="info.list" stripe border style="width: 100%">
    <el-table-column type="index" align="center" label="序号" width="80" />
    <el-table-column prop="productName" align="center" label="产品名称" width="150" />
    <el-table-column prop="loanProductType.loanTypeName" align="center" label="贷款类型" width="120" />
    <el-table-column prop="bankInfo.bankName" align="center" label="银行" width="100" />
    <el-table-column prop="interestRate" align="center" label="利率" width="60" />
    <el-table-column prop="maxLimit" align="center" label="最高额度" width="90" />
    <el-table-column prop="maxTerm" align="center" label="最长期限" width="80" />
    <el-table-column prop="otherAttributes" align="center" label="备注信息" />
    <el-table-column label="操作" align="center" width="150">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.row.productId)">修改</el-button>
        <el-popconfirm @confirm="handleDelete(scope.row.productId)" title="你确定要删除吗？">
          <template #reference>
            <el-button size="small" type="danger">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <!--分页-->
  <br>
  <div class="demo-pagination-block">
    <el-pagination
        v-model:current-page="info.pageNum"
        v-model:page-size="info.pageSize"
        :page-sizes="[5, 10, 15, 20]"
        :small="false"
        :disabled="false"
        :background=true
        layout="total, sizes, prev, pager, next, jumper"
        :total="info.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>
  <!--窗口-->
  <el-dialog v-model="info.dialogVisible" :title="info.isAdd?'增加产品分类':'修改产品分类'" width="40%" :before-close="handleClose">
    <el-form :model="info.form" label-width="auto" style="max-width: 90%;margin-left: 20px">
      <el-form-item label="产品名称">
        <el-input v-model="info.form.productName" />
      </el-form-item>
      <el-form-item label="贷款类型">
        <el-select v-model="info.form.loanTypeId" placeholder="选择产品类型">
          <el-option v-for="item in info.loanProductTypeList" :label="item.loanTypeName" :value="item.loanTypeId" />
        </el-select>
      </el-form-item>
      <el-form-item label="银行">
        <el-select v-model="info.form.bankId" placeholder="选择银行">
          <el-option v-for="item in info.bankInfoList" :label="item.bankName" :value="item.bankId" />
        </el-select>
      </el-form-item>
      <el-form-item label="利率">
        <el-input v-model="info.form.interestRate" />
      </el-form-item>
      <el-form-item label="最高额度">
        <el-input v-model="info.form.maxLimit" />
      </el-form-item>
      <el-form-item label="最长期限">
        <el-input v-model="info.form.maxTerm" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="info.form.otherAttributes" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="persistence">
          保存信息
        </el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 上传文件使用 -->
  <!--窗口-->
  <el-dialog v-model="info.ImportDialogVisible" title="数据导入" width="40%" :before-close="handleClose11">
    <el-upload
        ref="upload"
        class="upload-demo"
        :action="info.ImportUrl"
        :limit="1"
        :on-exceed="handleExceed"
        :auto-upload="false"
        :before-upload="beforeUpload"
        :on-success="handleSuccess"
    >
      <template #trigger>
        <el-button type="primary">选择文件</el-button>
      </template>
      &nbsp;
      <el-button class="ml-3" type="success" @click="submitUpload">
        上传文件
      </el-button>
      <template #tip>
        <div class="el-upload__tip text-red">
          只能上传一个文件，类型为*.xlsx，重复上传会覆盖前 一个文件
        </div>
      </template>
    </el-upload>
  </el-dialog>

</template>

<style scoped>

</style>